import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Message;
import javax.jms.TextMessage;
import java.util.ArrayList;

public class MyListenerError implements MessageListener
{
    ArrayList<Double> arr = new ArrayList<Double>();

    int n = 0;
    int cnt = 0;
    double num = 0;
    double total = 0;
    double average = 0;
    double var = 0;

    public void onMessage(Message message)
    {
        try
        {
            ++n;
            num = Double.parseDouble(((TextMessage)message).getText());
            arr.add(num);
            total = 0;
            for (double d:arr)
            {
                total += d;
            }
            average = total/n;
            total = 0;
            for (double d:arr)
            {
                total += Math.pow(d-average, 2);
            }
            var = Math.sqrt(total/n);
            if (num>average + 3*var || num < average-3*var)
                ++cnt;
            System.out.println("Received a message: " + num + " error number: " + cnt);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
